package com.cd.order.controller;

import com.cd.common.controller.BaseController;
import com.cd.common.domain.AjaxResult;
import com.cd.form.jpa.domian.FormInstance;
import com.cd.order.api.OrderService;
import com.cd.order.dto.CameraOrderDto;
import com.cd.order.jpa.domain.CameraOrder;
import com.cd.order.jpa.domain.OrderRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/order")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    //全部订单获取
    @GetMapping("/getAllOrder")
    @PreAuthorize("@ss.hasCameraOrderPermissions()")
    public AjaxResult getAllOrder() {
        //如果formId name 均为null 则为按照权限全局搜索
        //如果name不为空 则为按名称搜索
        List<CameraOrder> result = orderService.getAllOrder();
        return success(result);
    }

    //新增订单 有id直接报错
    @PostMapping("/add")
    @PreAuthorize("@ss.hasCameraOrderPermissions()")
    public AjaxResult addOrder(@RequestBody CameraOrderDto cameraOrder) {
         orderService.addOrder(cameraOrder);
        return success();
    }

    //修改订单 无id直接报错
    @PostMapping("/update")
    @PreAuthorize("@ss.hasCameraOrderPermissions()")
    public AjaxResult updateOrder(@RequestBody CameraOrderDto cameraOrder) {
         orderService.updateOrder(cameraOrder);
        return success();
    }
    //获取指定订单记录
    @GetMapping("/getOrderRecord")
    @PreAuthorize("@ss.hasCameraOrderPermissions()")
    public AjaxResult getOrderRecord(@RequestParam Long orderId) {
        List<OrderRecord> result =orderService.getOrderRecord(orderId);
        return success(result);
    }
}
